package com.yupi.yuojcodesandbox.docker;

import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.command.*;
import com.github.dockerjava.api.model.PullResponseItem;
import com.github.dockerjava.core.DefaultDockerClientConfig;
import com.github.dockerjava.core.DockerClientBuilder;
import com.github.dockerjava.core.DockerClientConfig;

public class DockerDemo {
    public static void main(String[] args) throws InterruptedException {
        // 配置Docker客户端连接到守护进程的地址
        DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder()
                .withDockerHost("tcp://192.168.88.100:2375")
                // 或者对于Windows，如果你已经设置了TCP连接，可以是 "tcp://localhost:2375"
                .build();
        // 使用配置创建Docker客户端实例
        DockerClient dockerClient = DockerClientBuilder.getInstance(config).build();
//        // 测试连接
//        PingCmd pingCmd = dockerClient.pingCmd();
//        pingCmd.exec();
        String image="nginx:latest";
        //拉取镜像
//        PullImageCmd pullImageCmd = dockerClient.pullImageCmd(image);
//        PullImageResultCallback pullImageResultCallback = new PullImageResultCallback(){
//            @Override
//            public void onNext(PullResponseItem item){
//                System.out.println("下载镜像:"+item.getStatus());
//                super.onNext(item);
//            }
//        };
//        pullImageCmd
//                .exec(pullImageResultCallback)
//                .awaitCompletion();
//        System.out.println("下载完成");

        //创建容器
//        CreateContainerCmd containerCmd = dockerClient.createContainerCmd(image);
//        CreateContainerResponse createContainerResponse=containerCmd
//                .withCmd("echo","hello Docker")
//                .exec();
//        System.out.println(createContainerResponse);
//
//       String containerId=createContainerResponse.getId();
//        //启动容器
//         dockerClient.startContainerCmd(containerId).exec();
        //删除容器
//         String continerID="ec6ca1077c12";
//        dockerClient.removeContainerCmd(continerID).withForce(true).exec();
//
//        continerID="a3331104c679";
//        dockerClient.removeContainerCmd(continerID).withForce(true).exec();

        //删除镜像
//        dockerClient.removeImageCmd(image).exec();


    }

}
